<?php

/**
 * IndexController
 *
 * @package
 * @author darche
 * @copyright Copyright (c) 2009
 * @version $Id$
 * @access public
 */
class AdminController extends Hoa_Controller_Action_Standard {

	private $formAddArticle;
	private $formAddVideo;
	private $formAddImage;
	private $arrayData = array();

	/**
	 * IndexController::IndexAction()
	 *
	 * @return
	 */

	private function showArrayData(){
		if(DEBUG) print_r($this->arrayData);
	}
	public function indentifyUserAction(){
		require_once('Users/User.php');
		extract($_GET);
		$email = 'admin';
		$pwd = 'admin';
		$conf = array(
		    'email' => $email,
		    'pwd' => $pwd
		);
		$user = new User($conf);
		$user->verifConncetion();
	}
	public function footer(){
		$this->view->config = $this->config;
		$this->view->head 	= $this->head;
		$this->view->config = $this->config;
		echo $this->view->render();
	}
	public function IndexAction(){
		$this->view->config = $this->config;
		$this->view->titre = 'Administration';
		$this->view->head 	= $this->head;
		self::indentifyUserAction();
		echo $this->view->render();
	}

//region Gestion Articles
	public function GestionArticlesAction(){
		$this->head->addCssFile($this->config->rep->cssFile.'/admin.css');
		$this->head->addJsFile($this->config->rep->jsFile.'/actionAdmin.js');
		self::addArticle();
		self::listingArticles();
		echo $this->view->render();
	}
	private function addArticle(){
		$this->formAddArticle = new formAddArticle();
		$this->view->form = $this->formAddArticle;
		$this->view->config = $this->config;
		$this->view->titre = 'Gestion des articles';
		if($this->formAddArticle->isValid()) {
			self::valideFormAddArticles();
		}
		$this->view->head 	= $this->head;
		$this->view->config = $this->config;

	}
	private function valideFormAddArticles(){
		$this->arrayData = $_POST;
		self::showArrayData();
		require_once('Admin/GestionArticles.php');
		$gestionArticles = new GestionArticles();
		$gestionArticles->add($this->arrayData);
	}
	private function listingArticles(){
		import('Cache.~');
		# Gestion du cache
		$frontendOptions = array(
			'lifetime' => 600
		);
		$backendOptions = array(
			'cache_directory' => $this->config->rep->cacheDirectory.'/'
		);
		$cache = Hoa_Cache::factory('Class', 'File', $frontendOptions, $backendOptions);
		$cache->setObject('listingArticles');
		$articles = $cache->showAllForListingAdmin();
		$this->view->articles = $articles;
	}
//endregion

//region Gestion Videos
	public function GestionVideosAction(){
		$this->head->addCssFile($this->config->rep->cssFile.'/admin.css');
		$this->head->addJsFile($this->config->rep->jsFile.'/actionAdmin.js');
		self::addVideos();
		self::listingVideos();
		self::footer();
	}
	private function addVideos(){
		$this->formAddVideo = new FormAddVideo();
		$this->view->form = $this->formAddVideo;
		$this->view->titre = 'Gestion des vidéos';
		if($this->formAddVideo->isValid()) {
			self::valideFormAddVideo();
		}
	}
	private function valideFormAddVideo(){
		$this->arrayData = $_POST;
		self::showArrayData();
		require_once('Admin/GestionVideos.php');
		$gestionVideos = new GestionVideos();
		$gestionVideos->add($this->arrayData);
	}
	private function listingVideos(){
		import('Cache.~');
		# Gestion du cache
		$frontendOptions = array(
			'lifetime' => 600
		);
		$backendOptions = array(
			'cache_directory' => $this->config->rep->cacheDirectory.'/'
		);
		$cache = Hoa_Cache::factory('Class', 'File', $frontendOptions, $backendOptions);
		$cache->setObject('listingVideos');
		$videos = $cache->showAllVideos();
		$this->view->videos = $videos;
	}
	public function DeleteVideoAction(){
		require_once('Admin/GestionVideos.php');
		$gestionVideos = new GestionVideos();
		try {
			$gestionVideos->delete($_POST['id']);
			echo 1;
		} catch(Hoa_Exception $e){
			$e->raiseError();
		}
	}
//endregion

//region Gestion Images
	public function GestionImagesAction(){
		self::addImage();
	}
	private function addImage(){
		$this->formAddImage = new FormAddImage();
		$this->view->form = $this->formAddImage;
		$this->view->titre = 'Gestion des images';
		if($this->formAddImage->isValid()) {
			self::valideFormAddImage();
		}
		self::footer();
	}
	private function valideFormAddImage(){
		import('StdClass.~');
		$this->arrayData = new Hoa_StdClass($_FILES['photo']);
		self::showArrayData();
		try {
			require_once('Admin/GestionImages.php');
			$gestionPhotos = new GestionImages();
			$gestionPhotos->add($this->arrayData);
		}catch(Hoa_Image_Exception $e){
			$e->raiseError();
		}

	}
//endregion

}